﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class member_page : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // get userid from query string
            int uid = 0;
            int.TryParse(Request.QueryString["uid"], out uid);

            //get user information
            DTO.Member member = MemberProcessorInstance.GetMemberById(uid);

            //set page title
            Page.Title = member.Username + "'s Profile at bookclubit.com";

            //declare labels
            lblUsername.Text = member.Username;
            lblUserName2.Text = member.Username;
     
            lblCreatedDate.Text = member.CreatedDate.ToString();
            imgMemberAvatar.ImageUrl = "avatars/" + member.AvatarURL;
          //  lblBiography.Text = member.Biography.ToString();


            DTO.BookClubs clubs = BookClubProcessorInstance.GetMemberBookClubs(uid);
            grdMemberBookClubs.DataSource = clubs;
            grdMemberBookClubs.DataBind();


        }

        public void BindData(DTO.BookClubs clubs)
        {
            grdMemberBookClubs.DataSource = clubs;
            grdMemberBookClubs.DataBind();
        }

        protected void grdMemberBookClubs_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {


            DTO.BookClub club = e.Row.DataItem as DTO.BookClub;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgClubAvatar = ((Image)e.Row.FindControl("imgClubAvatar"));
                HyperLink lnkBookClub = ((HyperLink)e.Row.FindControl("lnkBookClub"));
                HyperLink lnkViewClub = ((HyperLink)e.Row.FindControl("lnkViewClub"));
                Label lblClubType = ((Label)e.Row.FindControl("lblClubType"));
                Label lblNumMembers = ((Label)e.Row.FindControl("lblNumMembers"));
                Label lblCurrentBook = ((Label)e.Row.FindControl("lblCurrentBook"));
                Label lblCreatedDate = ((Label)e.Row.FindControl("lblCreatedDate"));

                int clubTypeInt = club.ClubType;
                int clubId = club.Id;
                //    string currentASIN = club.CurrentBook;

                DTO.ClubType type = BookClubProcessorInstance.GetBookClubType(clubTypeInt);

                DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(clubId);

                //    DTO.Book book = BookProcessorInstance.GetBookByASIN();

                imgClubAvatar.ImageUrl = "avatars/club/" + club.AvatarURL;
                lnkBookClub.Text = club.Name;
                lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
                lnkViewClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
                lblCurrentBook.Text = "";
                lblCreatedDate.Text = club.CreatedDate.ToString("MMM dd, yyyy") + " (" + TimeAgo(club.CreatedDate) + ")";

                if (type.ClubTypeName == "Public")
                    lblClubType.CssClass = "status public";

                if (type.ClubTypeName == "Private")
                    lblClubType.CssClass = "status private";

                lblClubType.Text = type.ClubTypeName;

                lblNumMembers.Text = members.Count.ToString();

            }

        }
        static string TimeAgo(DateTime time)
        {
            DateTime startDate = DateTime.Now;
            TimeSpan deltaMinutes = startDate.Subtract(time);
            string distance = string.Empty;
            if (deltaMinutes.TotalMinutes <= (8724 * 60))
            {
                distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
                if (deltaMinutes.Minutes < 0)
                {
                    return distance + " from  now";
                }
                else
                {
                    return distance + " ago";
                }
            }
            else
            {
                return "on " + time.ToString();
            }
        }
        static string DistanceOfTimeInWords(double minutes)
        {
            if (minutes < 1)
            {
                return "less than a minute";
            }
            else if (minutes < 50)
            {
                return Math.Round(minutes).ToString() + " minutes";
            }
            else if (minutes < 90)
            {
                return "about one hour";
            }
            else if (minutes < 1080)
            {
                return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
            }
            else if (minutes < 1440)
            {
                return "one day";
            }
            else if (minutes < 2880)
            {
                return "about one day";
            }
            else
            {
                return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
            }
        }

    }
}